package hot;

/**
 * @author way
 * @create 2025-03-13 21:31
 */
public class _543_diameterOfBinaryTree {
    int ans;

    public int diameterOfBinaryTree(TreeNode root) {
        if (root == null) return 0;
        dfs(root);
        return ans;
    }

    private int dfs(TreeNode root) {
        if (root == null) return 0;

        int lLen = dfs(root.left);
        int rLen = dfs(root.right);
        ans = Math.max(ans, lLen + rLen);
        return Math.max(lLen, rLen) + 1;
    }
}
